package com.estar.integaration.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estar.integaration.Mapper.MaterialMapper;
import com.estar.integaration.Service.IMaterialService;
import com.estar.integaration.Service.IMaterialVoService;
import com.estar.integaration.Util.DateUtil;
import com.estar.integaration.entity.Material;
import com.estar.integaration.entity.MaterialVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *  服务实现类
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

  @Autowired
  MaterialMapper materialMapper;

  @Autowired
  IMaterialService materialService;

  @Autowired
  IMaterialVoService materialVoService;
  /**
   * 功能： 根据id删除material记录
   * @param id
   * @return material
   * formshandler里有个多余的方法selectPurById功能和这个重复
   */
  @Override
  public Material getOneMaterialById(String id) {
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("id",id);
    Material material =  materialMapper.selectOne(queryWrapper);
    return material;
  }

  /**
   * 通过id找purchase
   * @param id
   * @return
   */
  @Override
  public Material selectPurById(String id){
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("id",id);
    Material material = materialMapper.selectById(id);
    return material;
  }

  @Override
  public void deleteAllByProductId(String productId) {
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("product_id",productId);
    List<Material> materialList = materialMapper.selectList(queryWrapper);
    List<MaterialVo> materialVoList = materialVoService.getMaterialVOList(queryWrapper);
    List<String> deleteList = new ArrayList<>();
    materialList.forEach(material -> deleteList.add(material.getId()));

    if(deleteList.size() != 0){
      materialMapper.deleteBatchIds(deleteList);
    }

    if(materialVoList.size() != 0){
      materialVoService.deleteById(materialVoList);
    }
  }

  @Override
  public List<Material> findAllMaterialInProductandCart() {
    return null;
  }

  @Override
  public List<Material> selectListByDateOneMonth() {
    Date lastMonthDate = DateUtil.getOneMonthBeforeDate();
    //查询近一个月的数据
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.ge("delivery",lastMonthDate);
    queryWrapper.le("delivery",new Date());
    List<Material> materialList = materialMapper.selectList(queryWrapper);
    return materialList;
  }


  /**
   * 功能： 更新物料
   * @param material
   * @param updateWrapper
   */
  @Override
  public void updateMaterial(Material material, UpdateWrapper updateWrapper) {
    materialMapper.update(null,updateWrapper);
  }

  /**
   * 功能： 插入又给material对象
   * @param material
   */
  @Override
  public void insertMaterial(Material material) {
    materialMapper.insert(material);
  }

  /**
   * 功能：根据已购筛选出物料
   * @param purchased
   * @return
   */
  @Override
  public List<Material> getMaterialByPurchased(Integer purchased) {
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("purchased",purchased);
    List<Material> materialList = materialMapper.selectList(queryWrapper);
    return materialList;
  }

  /**
   * 功能：根据物料的id更新物料的未购和已购
   * @param purchased
   * @param unPurchased
   * @param id
   */
  @Override
  public void updateMaterial(Integer purchased, Integer unPurchased, String id) {
    UpdateWrapper updateWrapper = new UpdateWrapper();
    updateWrapper.eq("id",id);
    updateWrapper.set("purchased",purchased);
    updateWrapper.set("unpurchased",unPurchased);
    materialMapper.update(null,updateWrapper);
  }

  /**
   * 功能： 根据id更新采购部图标的未购和计划用量
   * @param id
   * @param unPurchase
   * @param BOMQuantity
   */
  @Override
  public void updateMaterialUnPurchased(String id, Integer unPurchase, Integer BOMQuantity) {
    UpdateWrapper updateWrapper = new UpdateWrapper();
    updateWrapper.eq("id",id);
    updateWrapper.set("unpurchased",unPurchase);
    updateWrapper.set("quantity",BOMQuantity);
    materialMapper.update(null,updateWrapper);
  }

  /**
   * 功能：根据物料号和机台号查找出material
   * @param MaterialNo
   *        EquipmentNo 变更为aCode
   * @return
   * formshandler里有个多余的 selectPurchaseDepartByNoEquipNo
   */
  @Override
  public Material getPByMaNoandENo(String MaterialNo, String aCode) {
    HashMap<String,String> queryHash = new HashMap<>();
    queryHash.put("material_no",MaterialNo);
    queryHash.put("equipment_no",aCode);
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.allEq(queryHash);
    Material material = materialMapper.selectOne(queryWrapper);
    return material;
  }

  /**
   * 去库存业务，清空unpurchased，记录inventory
   * @param id
   */
  @Override
  public void updateUnPurchasedToZero(String id) {
    Material material = materialMapper.selectById(id);
    UpdateWrapper updateWrapper = new UpdateWrapper();
    updateWrapper.eq("id",id);
    material.setInventory(material.getUnpurchased());
    material.setUnpurchased(0d);
    materialMapper.update(material,updateWrapper);
  }
  /**
   * 撤销用库存
   * @param id
   */
  @Override
  public void updateCancelUseInventory(String id) {
    Material material = materialMapper.selectById(id);
    Double inventory = material.getInventory();
    UpdateWrapper updateWrapper = new UpdateWrapper();
    updateWrapper.eq("id",id);
    material.setInventory(material.getUnpurchased());
    material.setUnpurchased(0d);
    materialMapper.update(material,updateWrapper);
  }
  /**
   * 功能： 查找出表中所有的material
   * @param queryWrapper
   * @return List<Material>
   */
  @Override
  public List<Material> selectList(QueryWrapper queryWrapper) {
    return materialMapper.selectList(null);
  }

  /**
   * 根据MaterialNo和Time找到material
   * @param materialNo
   * @param delivery
   * @return
   */
  @Override
  public Material selectMaterialByNoTime(String materialNo, String delivery) {
    Map<String,String> queryMap = new HashMap<>();
    queryMap.put("material_no",materialNo);
    queryMap.put("delivery",delivery);
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.allEq(queryMap);
    Material material = materialMapper.selectOne(queryWrapper);//更新部分字段
    return material;
  }

  @Override
  public List<Material> selectListByproductId(String productId) {
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("product_id",productId);
    return materialMapper.selectList(queryWrapper);
  }

  @Override
  public List<Material> selectListByProductACode(String aCode) {
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("a_code",aCode);
    return materialMapper.selectList(queryWrapper);
  }

  @Override
  public List<Material> selectChildACodeMaterialListByproductId(String productId) {

    return null;
  }

}
